/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of libgp2x.

    libgp2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libgp2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libgp2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>

#include "gp2x_internal.h"

pid_t pid940 = 0;

void GP2x_940_load(char* filename) {
#ifdef MANUAL940
    printf("Please load './%s %s %s' then press any key.\n", filename, GP2x_register_file, GP2x_memory_file);
    getc(stdin);
    GP2x_940_running = 1;
    printf("Running.\n");
#else
    printf("forking\n");
    GP2x_940_running = 1;
    pid940 = fork();
    printf("forked %i\n", (int)pid940);
    if(pid940 == 0) {
#ifdef VALGRIND940
        char* tmpfile = malloc(strlen(filename) + 3);
        sprintf(tmpfile, "./%s", filename);
        printf("execing valgrind %s %s %s\n", tmpfile, GP2x_register_file, GP2x_memory_file);
        execl("/usr/bin/valgrind", "valgrind", tmpfile, GP2x_register_file, GP2x_memory_file, NULL);
        free(tmpfile);
#else
        printf("execing %s %s %s\n", filename, GP2x_register_file, GP2x_memory_file);
        execl(filename, filename, GP2x_register_file, GP2x_memory_file, NULL);
#endif
    }
#endif
}

void GP2x_940_shutdown() {
    if(pid940 != 0) {
        kill(pid940, 9);
        GP2x_940_running = 0;
        printf("Killed 940t\n");
    }
}

void GP2x_940_reset() {
}
